package com.intretech.umsin.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intretech.umsin.demo.entity.OrderEntity;
import com.intretech.umsin.demo.mapper.OrderMapper;
import com.intretech.umsin.demo.param.OrderParam;
import com.intretech.umsin.demo.service.OrderService;
import com.intretech.umsin.demo.vo.OrderVo;
import com.intretech.umsin.demo.vo.converter.OrderVoEntityConverter;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;



/**
* @version V1.0
* 订单:order 对应商业服务实现.
* @ClassName: OrderServiceImpl
* @Description: 订单:order 对应商业服务实现.
* @author: 李启联
* @date:   2022-5-28 23:00:30
* @Copyright: Copyright (c) 2022
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements OrderService {

    @Autowired
    private OrderVoEntityConverter orderVoEntityConverter;


    /**
    * 根据ID查询单个订单对象
    * @param id 组键ID
    * @return 单个订单VO对象
    */
    public OrderVo queryById(Serializable id){
        if (id == null) {return null;}
        return orderVoEntityConverter.reverseFromTarget(this.getById(id));
    }


    /**
    * 根据ID集合查询返回订单列表
    * @param idList 组键ID集合
    * @return 订单列表
    */
    public List<OrderVo> queryByIds(Collection<? extends Serializable> idList){
        if(CollectionUtils.isEmpty(idList)){return null;}
        return orderVoEntityConverter.reverseFromTarget(this.listByIds(idList));
    }


    /**
    * 分页查询 订单 信息
    * @param page 分页对象
    * @param param 查询参数
    * @return IPage<OrderVo> 分页对象
    */
    @Override
    public IPage<OrderVo> queryForPage(IPage page,OrderParam param){
      IPage<OrderEntity> page0 = this.page(page,createQueryWrapper(param));
      IPage<OrderVo> pageResult = page0.convert(e -> orderVoEntityConverter.reverseFromTarget(e));
      return pageResult;
    }


    /**
    * 根据条件查询返回 订单 对象列表
    * @param  param 查询参数
    * @return 订单 对象列表
    */
    @Override
    public List<OrderVo> queryForList(OrderParam param) {
        List<OrderEntity> list = this.list(createQueryWrapper(param));
        return orderVoEntityConverter.reverseFromTarget(list);
    }

    /**
    * 构建查询条件
    * @param param 查询参数实体对象
    * @return 构建后的QueryWrapper查询条件
    */
    private QueryWrapper<OrderEntity> createQueryWrapper(OrderParam param){
        QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(param)) { return queryWrapper; }
        Optional<OrderParam> op = Optional.of(param);
        queryWrapper.lambda()
                .eq(op.map(OrderParam::getId).filter(StringUtils::isNotBlank).isPresent(),OrderEntity::getId,param.getId())
                .eq(op.map(OrderParam::getOrderNo).filter(StringUtils::isNotBlank).isPresent(),OrderEntity::getOrderNo,param.getOrderNo())
                .eq(op.map(OrderParam::getCustomerId).filter(StringUtils::isNotBlank).isPresent(),OrderEntity::getCustomerId,param.getCustomerId())
                .eq(op.map(OrderParam::getReceiverId).filter(StringUtils::isNotBlank).isPresent(),OrderEntity::getReceiverId,param.getReceiverId())
                .eq(op.map(OrderParam::getOrderStatus).isPresent(),OrderEntity::getOrderStatus,param.getOrderStatus())
        ;
        return queryWrapper;
    }

    /**
    * 添加订单到数据库
    * @param vo  订单 VO对象
    * @return 添加后的订单 VO对象
    */
    @Override
    public OrderVo add(OrderVo vo) {
        if (ObjectUtils.isEmpty(vo)) {return null;}
        OrderEntity entity = orderVoEntityConverter.convertToTarget(vo);
        if(this.save(entity)){
            return orderVoEntityConverter.reverseFromTarget(this.getById(entity.getId()));
        }else {
            return null;
        }
    }

    /**
    * 添加订单集合列表到数据库
    * @param vos  订单 VO对象列表
    * @return 添加后的订单 VO对象集合列表
    */
    @Override
    public List<OrderVo> adds(List<OrderVo> vos) {
        if (CollectionUtils.isEmpty(vos)) { return null; }
        List<OrderEntity> entities = orderVoEntityConverter.convertToTarget(vos);
        if(this.saveBatch(entities)){
            List<Serializable> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return orderVoEntityConverter.reverseFromTarget(this.listByIds(ids));
        }else {
           return null;
        }
    }

    /**
    * 更新订单到数据库
    * @param vo 要更新的订单 VO对象
    * @return 更新后的订单 VO对象
    */
    @Override
    public OrderVo modify(OrderVo vo) {
        if (ObjectUtils.isEmpty(vo)) {return null;}
        OrderEntity entity = orderVoEntityConverter.convertToTarget(vo);
        if(this.updateById(entity)){
            return orderVoEntityConverter.reverseFromTarget(this.getById(entity.getId()));
        }else {
            return null;
        }
    }

    /**
    * 更新订单集合列表到数据库
    * @param vos 要更新的订单 VO对象集合
    * @return 更新后的订单 VO对象集合
    */
    @Override
    public List<OrderVo> modifys(List<OrderVo> vos) {
        if (CollectionUtils.isEmpty(vos)) { return null; }
        List<OrderEntity> entities = orderVoEntityConverter.convertToTarget(vos);
        if(this.updateBatchById(entities)){
            List<Serializable> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return orderVoEntityConverter.reverseFromTarget(this.listByIds(ids));
        }else {
            return null;
        }
    }

    @Override
    public List<OrderEntity> selectOrderById(String id) {
        return null;
    }
}

